package com.douqu.game.core.entity.ext.data.major;

import com.bean.core.buffer.ByteBuffer;
import com.douqu.game.core.config.StableDataConfig;
import com.douqu.game.core.config.common.GoodsData;
import com.douqu.game.core.config.major.MajorConfig;
import com.douqu.game.core.config.major.MajorMergeConfig;
import com.douqu.game.core.e.E_StableDataType;
import com.douqu.game.core.entity.Player;
import com.douqu.game.core.entity.ext.BagInfo;
import com.douqu.game.core.entity.ext.data.BaseData;
import com.douqu.game.core.factory.ConstantFactory;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.protobuf.SGCommonProto;
import com.douqu.game.core.util.CoreUtils;
import com.douqu.game.core.util.LogUtils;

import java.util.List;

/**
 * @author wangzhenfei
 *         2017-10-18 18:38
 *         个人的炼制信息
 */
public class RefiningData extends BaseData {
    /**
     * 炼制等级
     */
    private int level;

    private Player player;
    public RefiningData(Player player)
    {
        this.player = player;
    }

    @Override
    public void init()
    {
        calculateLevel();
    }

    @Override
    public void checkInit() {
        if(level == 0){
            init();
        }
    }


    @Override
    public void reset()
    {

    }

    @Override
    public void checkReset() {
        if(!CoreUtils.isTodayByOffset(DataFactory.currentTime, DataFactory.currentTime, ConstantFactory.DEFAULT_RESET_FRESH_TIMES_HOUR)){
            reset();
        }
    }


    @Override
    public void writeTo(ByteBuffer buffer)
    {
        buffer.writeShort(level);
        buffer.writeBoolean(false);

    }

    @Override
    public void loadFrom(ByteBuffer buffer)
    {
        level = buffer.readShort();
        buffer.readBoolean();
        calculateLevel();
    }

    public boolean hasRedPointRemind() {
        BagInfo bagInfo = player.getExtInfo(BagInfo.class);
        List<MajorMergeConfig> list = DataFactory.getInstance().getDataList(DataFactory.MAJOR_MERGE_KEY);
        int sum = 0;
        for(MajorMergeConfig config : list){
            sum = 0;
            if(getLevel() >= config.needSkillLv){
                for(int id : config.srcMaterial){
                    if(bagInfo.getPropCount(id) > 0){
                        sum ++;
                    }
                    if(sum >= 3){
                        return true;
                    }
                }
            }

        }
        return false;
    }

    public boolean isMajorRefining(){
        GoodsData temp = getExpAsset();
        BagInfo bagInfo = player.getExtInfo(BagInfo.class);
        int count = bagInfo.getGoodsCount(temp.type, temp.id);
        if (count > 0){
            return true;
        }
        return false;
    }

    /**
     * 重新计算等级
     */
    public void calculateLevel() {
        GoodsData temp = getExpAsset();
        BagInfo bagInfo = player.getExtInfo(BagInfo.class);
        int count = bagInfo.getGoodsCount(temp.type, temp.id);
        List<MajorConfig> majorConfigs = DataFactory.getInstance().getDataList(DataFactory.MAJOR_KEY);
        int end = majorConfigs.size();
        int tempLv = 0;
        for(int i = 0; i < end; i++)
        {
            if(count < majorConfigs.get(i).refiningExp)
                break;
            tempLv = majorConfigs.get(i).id;
        }

        //重新计算对象
        this.level = tempLv;
    }

    public GoodsData getExpAsset(){
        StableDataConfig config = DataFactory.getInstance().getGameObject(DataFactory.STABLE_DATA_KEY, E_StableDataType.MAJOR_EXP.getCode());
        GoodsData temp = null;
        for(GoodsData data : config.goods){
            if(data.type == SGCommonProto.E_MAJOR_TYPE.MAJOR_TYPE_REFINING_VALUE){
                temp = new GoodsData(SGCommonProto.E_GOODS_TYPE.GOODS_TYPE_ASSETS_VALUE, data.id, data.value);
                break;
            }
        }
        return temp;
    }

    public int getLevel() {
        if(level == 0){
            calculateLevel();
        }
        return level;
    }
}
